package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.JwtTool;
import com.jzo2o.customer.mapper.WorkerCertAuditMapper;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.service.IWorkerCertAuditService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkerCertAuditServiceImpl extends ServiceImpl<WorkerCertAuditMapper, WorkerCertificationAudit> implements IWorkerCertAuditService {
    @Resource
    private JwtTool jwtTool;

    /**
     * 服务人员实名认证功能
     * @param dto 实名信息
     * @param request request
     */
    @Override
    public void WorkeRealNameAuthentication(WorkerCertificationAuditAddReqDTO dto, HttpServletRequest request) {
        // 从请求头中解析出token
        String token = request.getHeader("Authorization");
        // 根据token解析出当前登录的用户信息(userId)
        CurrentUserInfo user = jwtTool.parseToken(token);
        // 封装数据
        WorkerCertificationAudit workerCertAudit = BeanUtils.copyBean(dto, WorkerCertificationAudit.class);
        workerCertAudit.setServeProviderId(user.getId());
        workerCertAudit.setCreateTime(new Date());
        baseMapper.insert(workerCertAudit);
    }

    /**
     * 查询当前服务人员用户实名认证的驳回原因功能
     * @param request request
     * @return Map
     */
    @Override
    public Map<String, String> rejectReason(HttpServletRequest request) {
        // 从请求头中解析出token
        String token = request.getHeader("Authorization");
        // 根据token解析出当前登录的用户信息(userId)
        CurrentUserInfo user = jwtTool.parseToken(token);
        // 根据用户id查询用户实名认证的驳回原因
        WorkerCertificationAudit workerCertAudit = baseMapper.selectOne(
                new LambdaQueryWrapper<WorkerCertificationAudit>()
                        .eq(WorkerCertificationAudit::getServeProviderId, user.getId())
        );
        // 封装数据
        Map<String, String> mapResult = new HashMap<>();
        mapResult.put("rejectReason", workerCertAudit.getRejectReason());
        return mapResult;
    }

    /**
     * 服务人员实名认证
     * @param id  id
     * @param wca 认证参数信息
     */
    @Override
    public void audit(Long id, WorkerCertificationAudit wca) {
        WorkerCertificationAudit workerCertAudit = baseMapper.selectById(id);
        if(workerCertAudit == null) {
            throw new ForbiddenOperationException("您所认证的实名信息不存在,请检查");
        }
        // 封装数据
        workerCertAudit.setCertificationStatus(wca.getCertificationStatus());
        workerCertAudit.setRejectReason(wca.getRejectReason());
        workerCertAudit.setAuditStatus(1);
        // 更新
        baseMapper.updateById(workerCertAudit);
    }
}
